﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CodeGame.Networking.Serverside;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CodeGame.ServerObjects
{
    internal class Entity
    {
        private string PrivateID;
        private string PublicID;
        protected Player Owner;

        public int X = 0;
        public int Y = 0;
        private int MovementX = 0;
        private int MovementY = -1;

        public int Angle = 0;

        public string Texture = "entity";
        public int TintR = 255;
        public int TintG = 255;
        public int TintB = 255;

        public Entity(string id, Player ply)
        {
            this.PublicID = ServerSingletons.GetUID(5);
            this.PrivateID = id;
            this.Owner = ply;

            ServerProtocolEntity.CreateEntity(this.PublicID, this.X, this.Y, this.Angle, this.Texture, this.TintR, this.TintG, this.TintB);

            this.UpdateMovement();
        }

        public virtual void Spawn(string id, int x, int y)
        {
            this.X = x;
            this.Y = y;
            this.UpdateNetworkEntity();
        }

        public virtual void Move(string id, int amount)
        {
            if (id != this.PrivateID)
	        {
                return;
            }
            this.X += (int)(this.MovementX * amount);
            this.Y += (int)(this.MovementY * amount);
            this.UpdateNetworkEntity();
        }

        public string GetID()
        {
            return this.PublicID;
        }

        public string GetPID()
        {
            return this.PrivateID;
        }

        public virtual void Turn(string id)
        {
            if (id != this.PrivateID)
	        {
                return;
            }

            this.Angle += 90;
            if (this.Angle > 300)
            {
                this.Angle = 0;
            }
            this.UpdateMovement();
            this.UpdateNetworkEntity();
        }

        public virtual void Tint(string id, int r, int g, int b)
        {
            if (id != this.PrivateID)
	        {
                return;
            }

            this.TintR = r;
            this.TintG = g;
            this.TintB = b;
            this.UpdateNetworkVisual();
        }

        public virtual void TextureEnt(string id, string texture)
        {
            if (id != this.PrivateID)
	        {
                return;
            }
            
            this.Texture = texture;
            this.UpdateNetworkVisual();
        }

        public void UpdateMovement()
        {
            switch (this.Angle)
            {
                case 0:
                    this.MovementX = 0;
                    this.MovementY = -1;
                    break;
                case 90:
                    this.MovementX = 1;
                    this.MovementY = 0;
                    break;
                case 180:
                    this.MovementX = 0;
                    this.MovementY = 1;
                    break;
                case 270:
                    this.MovementX = -1;
                    this.MovementY = 0;
                    break;
                default:
                    break;
            }
        }

        public virtual void MakeOwner(string id, Player ply)
        {
            if (id != this.PrivateID)
            {
                return;
            }

            this.Owner = ply;
        }

        public virtual void Update()
        {

        }

        public virtual void UpdateNetworkEntity()
        {
            ServerProtocolEntity.TeleportEntity(this.PublicID, this.X, this.Y, (float)this.Angle);
        }

        public virtual void UpdateNetworkVisual()
        {
            ServerProtocolEntity.UpdateEntityVisual(this.PublicID, this.Texture, this.TintR, this.TintG, this.TintB);
        }
    }
}
